package com.kdtech.analyse.outside;
import java.util.Locale;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @author KK
 *
 */
public class UyghurJAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*uyghur-j.org/.*/[0-9]*/[0-9]*/.*",
		"http://.*uyghur-j.org/[0-9]*/[0-9]*/.*",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("title").text();
		content=HtmlCleaner.getContentHtml(url,doc.select(".entry-content"));
//		if (date==null)date =JSoupUtils.matchDate(doc, "日期：");
		String text=doc.select("#content .entry-meta").text();
		if (date==null)date =DateUtils.matchDate(text);
		if(date==null){
			if(url.startsWith("http://uyghur-j.org/japan/tr/")){
				text=doc.select(":containsOwn(Yayınlanan:) > a").text();
				date=DateUtils.parseLong(text, "d MMM yyyy", Locale.forLanguageTag("tr"));
			}
		}
		if(date==null) date=DateUtils.matchDate(StringUtils.substringAfterLast(StringUtils.substringBeforeLast(url, "/"),"/"));
		author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
